import java.util.Arrays;

public class Test {

//    1.4 数组的使用
//1.4.1 数组中元素访问
    public static void main3(String[] args) {
        int[] arr1 = {1,2,3,4};
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
        System.out.println();


        for (int x:arr1) {
            System.out.print(x+" ");
        }
        System.out.println();
    }

    public static void main2(String[] args) {
//        如果没有对数组进行初始化，数组中元素有其默认值
        char[] arr1 = new char[10];
        double[] arr2 = new double[10];
        boolean[] arr3 = new boolean[10];
        String[] arr4 = new String[10];
    }
//1.3 数组的创建及初始化
    public static void main1(String[] args) {
        int[] arr1 = {1,2,3,4};
        int[] arr2 = new int[10]; //定义了一个大小为10的整型数组
        int[] arr3 = new int[]{1,2,3,4,5,6,7};

        System.out.println(arr1);
        System.out.println(arr2);
        System.out.println(arr3);

    int[] arr4;
    arr4 = new int[10];

    int[] arr5;
    arr5 = new int[]{1,2,3,4,5,6};

    //erro
    //int[] arr6;
    //arr6 = {1,2,3,4,5}
    }


    //数组转字符串
    public static void main4(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        String newarr = Arrays.toString(arr);
        System.out.println(newarr);


        // 使用Arrays中copyOf方法完成数组的拷贝：
        int[] copy = Arrays.copyOf(arr,arr.length*2);//扩容
        System.out.println(Arrays.toString(copy));

        int[] copy1 = Arrays.copyOfRange(arr,1,3);
        System.out.println(Arrays.toString(copy1));
    }

    public static void main5(String[] args) {
        int[] arr = {1,2,13,4,15};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        //二分查找指定元素索引
        System.out.println(Arrays.binarySearch(arr,15));

        //比较2个数字 对应位置 数据是否一样 ！
        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = {1,2,3,4,5};
        boolean flg = Arrays.equals(arr1,arr2);
        System.out.println(flg);

        //帮你直接填充数据！[1,4)
        int[] arr3 = new int[10];
        Arrays.fill(arr3,1,4,-1);
        System.out.println(Arrays.toString(arr3));
        System.out.println(Arrays.binarySearch(arr3, 13));

    }

//编写冒泡排序函数
   public static void bubbleSort(int[] array){
        //i代表趟数
       for (int i = 0; i < array.length-1; i++) {
           boolean flg = false;
           for (int j = 0; j < array.length-1-i; j++) {
               if(array[j]>array[j+1]){
                   int tmp = array[j];
                   array[j] = array[j+1];
                   array[j+1] = tmp;
                   flg = true;
               }
           }
           if(flg == false){
               break;//说明已经有序了！
           }
       }
   }


   //二分查找
    public static int binarySearch(int[] arr,int key){
        int r = 0;
        int l = arr.length-1;
        while(r<=l){
            int mid = (r+l)/2;
            if(arr[mid] > key){
                l = mid - 1;
            }
            else if (arr[mid] < key) {
                r = mid +1;
            }
            else{
                return mid;
            }
        }
        return -1;
    }



    public static void main(String[] args) {
        int[] arr = new int[]{8,9,3,5,48,56,1,50,0};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));

        int[] arr1 = new int[]{4,56,2,1,2,9,7,5,5,25,3};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));
        System.out.println(binarySearch(arr1,7));
    }


}


